/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nt;

import com.absisgroup.nsc.utilities.GenericServlet;
import com.absisgroup.nsc.utilities.Util;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/**
 *
 * @author Hue
 */
public class LuuMatHang extends GenericServlet {

    @Override
    public int process(final Connection con, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        BigDecimal prodPK = Util.toBigDecimal(request.getParameter("prodPK"));
        String code = request.getParameter("code");
        String nameBySupplier = request.getParameter("nameBySupplier");
        String nameForSell = request.getParameter("nameForSell");
        String madeIn = request.getParameter("madeIn");
        String type = request.getParameter("type");
        if(prodPK == null) {
            insert(con, code, nameBySupplier, nameForSell, madeIn, type);
        }
        else {
            update(con, prodPK, code, nameBySupplier, nameForSell, madeIn, type);
        }
        con.commit();
        
        final JSONObject jsObj = new JSONObject();
        jsObj.put("success", true);

        writeToResponse(response, jsObj.toString());

        return DONT_RUN_JSP;
    }

    @Override
    protected boolean useConnection() {
        return true;
    }
    
    private void insert(Connection con, String code, String nameBySupplier, String nameForSell, String madeIn, String type) throws SQLException {
        String strQuery = "insert into PRODUCTS(PROD_CODE, PROD_NAME_BY_SUPPLIER, PROD_FOR_SELL, PROD_MADE_IN, PROD_TYPE) values(?, ?, ?, ?, ?)";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        int iiIndex = 1;
        pStmt.setString(iiIndex++, code);
        pStmt.setString(iiIndex++, nameBySupplier);
        pStmt.setString(iiIndex++, nameForSell);
        pStmt.setString(iiIndex++, madeIn);
        pStmt.setString(iiIndex++, type);
        pStmt.executeUpdate();
        pStmt.close();
    }
    
    private void update(Connection con, BigDecimal prodPK, String code, String nameBySupplier, String nameForSell, String madeIn, String type) throws SQLException {
        String strQuery = "update PRODUCTS set PROD_CODE = ?, PROD_NAME_BY_SUPPLIER = ?, PROD_FOR_SELL = ?, PROD_MADE_IN = ?, PROD_TYPE = ? where PROD_PK = ?";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        int iiIndex = 1;
        pStmt.setString(iiIndex++, code);
        pStmt.setString(iiIndex++, nameBySupplier);
        pStmt.setString(iiIndex++, nameForSell);
        pStmt.setString(iiIndex++, madeIn);
        pStmt.setString(iiIndex++, type);
        pStmt.setBigDecimal(iiIndex++, prodPK);
        pStmt.executeUpdate();
        pStmt.close();
    }
}
